package com.example.easyexcel.service.impl;

import com.alibaba.excel.EasyExcel;
import com.example.easyexcel.dao.AssetInterfaceApiInfoMapper;
import com.example.easyexcel.dto.AssetInterfaceApiInfo;
import com.example.easyexcel.listener.AssetInterfaceApiInfoListener;
import com.example.easyexcel.service.AssetInterfaceApiInfoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

@Service
@RequiredArgsConstructor
@Slf4j
public class AssetInterfaceApiInfoServiceImpl implements AssetInterfaceApiInfoService {

    private final AssetInterfaceApiInfoMapper assetInterfaceApiInfoMapper;

    @Override
    public void importApiExcel(MultipartFile file) {
        try (InputStream inputStream = file.getInputStream()) {
            // 清空表数据（根据需求决定是否启用）
            // assetInterfaceApiInfoMapper.truncateTable();

            EasyExcel.read(inputStream, AssetInterfaceApiInfo.class,
                            new AssetInterfaceApiInfoListener(assetInterfaceApiInfoMapper))
                    .sheet()
                    .doRead();
        } catch (Exception e) {
            log.error("API接口Excel导入失败", e);
            throw new RuntimeException("API接口Excel导入失败", e);
        }
    }
}
